package com.csu.mining.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.csu.mining.R;
import com.dou361.ijkplayer.widget.PlayStateParams;
import com.dou361.ijkplayer.widget.PlayerView;

public class VideoAndDocumentActivity extends BaseActivity {

    private int order;
    PlayerView play;
    private void initData() {
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null && mBundle.getSerializable("order") != null) {
            order = (int) mBundle.getSerializable("order");
            Log.e("VideoAndDocumentActivity", "the order is " + order);
        } else {
            Log.e("VideoAndDocumentActivity", "the order is none");
            Toast t = Toast.makeText(this, "参数错误，即将返回", Toast.LENGTH_LONG);
            t.show();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            onBackPressed();
        }
    }

    protected void initDocument() {
        String[] wordUrls = {
                this.getString(R.string.word_url1), this.getString(R.string.word_url2),
                this.getString(R.string.word_url3), this.getString(R.string.word_url4),
                this.getString(R.string.word_url5), this.getString(R.string.word_url6),
                this.getString(R.string.word_url7), this.getString(R.string.word_url8)
        };
        WebView webView = fd(R.id.document_web_view);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
        });
        // webview必须设置支持Javascript才可打开
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置此属性,可任意比例缩放
        webView.getSettings().setUseWideViewPort(true);
        Log.e("DocumentActivity", "ready to load html");
        //通过在线预览office文档的地址加载
        webView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src=" + wordUrls[order]);
    }

    protected void initVideo(String title) {
        String[] videoUrls = {
                this.getString(R.string.video_url1), this.getString(R.string.video_url2),
                this.getString(R.string.video_url3), this.getString(R.string.video_url4),
                this.getString(R.string.video_url5), this.getString(R.string.video_url6),
                this.getString(R.string.video_url7), this.getString(R.string.video_url8)
        };
        String url = videoUrls[order];
        play = new PlayerView(this)
                .setTitle(title)//视频名称l);
                .setScaleType(PlayStateParams.fillparent)
                .hideMenu(true)
                .forbidTouch(false)
                .setPlaySource(url);
        play.startPlay();
        setFullScreen();
    }

    protected  void setFullScreen(){
        play.getFullScreenView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("order",order);
                intent.setClass(VideoAndDocumentActivity.this,VideoActivity.class);
                startActivity(intent);
                VideoAndDocumentActivity.this.overridePendingTransition(R.anim.no_animation,R.anim.no_animation);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        play.stopPlay();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_and_document);
        initData();
        String[] arrWays = {
                this.getString(R.string.way1), this.getString(R.string.way2),
                this.getString(R.string.way3), this.getString(R.string.way4),
                this.getString(R.string.way5), this.getString(R.string.way6),
                this.getString(R.string.way7), this.getString(R.string.way8)
        };
        initDocument();
        initVideo(arrWays[order]);
    }
}
